﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HouseManager.BLL;
using HouseManger.Model;

namespace HouseManager
{
    public partial class Update : System.Web.UI.Page
    {
        //声明一个变量来接收上一个页面传过来的id值
        public int id;
        //实例化类对象(用来调用方法)
        MangerBLL mangerBLL = new MangerBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request["id"]);
            if (!IsPostBack)
            {
                //调用方法给房屋类型下拉框绑定数据源
                drType.DataSource = mangerBLL.GetHouseType();
                drType.DataValueField = "Id";
                drType.DataTextField = "TName";
                drType.DataBind();

                //调用查询id方法 把接收到的ID值放进方法参数中
                HouseInfo info = mangerBLL.SelectId(id);
                //把根据id查询到的数据 显示在页面中
                txtName.Text = info.Name;
                txtArea.Text = info.Area.ToString();
                txtPrice.Text = info.Price.ToString();
                txtFloorCount.Text = info.FloorCount.ToString();
                txtFloor.Text = info.Floot.ToString();
                rState.SelectedIndex = info.State;
                txtYears.Text = info.Years.ToString();
                drType.SelectedValue = info.TypeId.ToString();
                txtDescribe.Text = info.Describe.ToString();
            }
        }
        //修改
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            HouseInfo info = new HouseInfo()
            {
                Id = id,
                Name = txtName.Text,
                Area = Convert.ToDecimal(txtArea.Text),
                Describe = txtDescribe.Text,
                Floot = Convert.ToInt32(txtFloor.Text),
                FloorCount = Convert.ToInt32(txtFloorCount.Text),
                Price = Convert.ToDecimal(txtPrice.Text),
                State = Convert.ToInt32(rState.SelectedIndex),
                TypeId = Convert.ToInt32(drType.SelectedValue),
                Years = Convert.ToDecimal(txtYears.Text)
            };
            //调用方法实现修改
            int res = mangerBLL.EditInfo(info);
            if (res > 0)
            {
                Response.Write("<script>alert('修改成功！');window.location.href='Default.aspx';</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败！');</script>");
            }
        }
    }
}